#include <stdlib.h>
#include <stdio.h>
#include "flit.h" 
#include "flit_queue.h" 

flit_t *read(flit_queue_t *queue)
{ 
  return queue->head; 
}  
   
flit_t *recv(flit_queue_t *queue)
{ 
  flit_t *flit_ptr;
  if(queue->head == NULL) 
  {
    printf("WARN: recv: queue underflow.\n");
    return NULL; 
  }  

  flit_ptr = queue->head; 
  queue->head = queue->head->next; 
  if(queue->head == NULL) 
    queue->tail = NULL; 
  
  flit_ptr->next = NULL; 
  queue->num_flits --; 
  return flit_ptr;
}  
  
   
void   send(flit_queue_t  *queue, flit_t **flit_ptr)
{ 
  if(queue->tail == NULL)
  { 
    queue->head       = *flit_ptr;
    queue->tail       = *flit_ptr;
  } 
  else 
  { 
    queue->tail->next = *flit_ptr;
    queue->tail       = *flit_ptr;
  }  

  (*flit_ptr)->next = NULL;
  *flit_ptr         = NULL; 
  queue->num_flits ++; 
} 

void   init_queue(flit_queue_t *queue)
{ 
  queue->head = NULL; 
  queue->tail = NULL;
  queue->num_flits = 0; 
}  
